#! /bin/sh

ERL_CRASH_DUMP=${ERL_CRASH_DUMP-/dev/null}
export ERL_CRASH_DUMP

cleanup () {
  erl -pa ../src -sname fragmentron_killah -eval '
    "fragmentron_killah@" ++ Host = atom_to_list (node ()),
    Other = list_to_atom ("fragmentron_change_target_test1@" ++ Host),
    MoreOther = list_to_atom ("fragmentron_change_target_test2@" ++ Host),
    rpc:call (Other, erlang, halt, []),
    rpc:call (MoreOther, erlang, halt, [])
  ' -noshell -noinput -s erlang halt 

  rm -rf Mnesia*
}

if test @HAVE_TCERL@ = yes && test @HAVE_MNESIA_EXT@ = yes
  then
    use_tcerl=true
  else
    use_tcerl=false
  fi

rm -rf Mnesia*

erl -pa ../src -sname fragmentron_change_target_test1 -s mnesia \
    -eval '
  case init:get_plain_arguments () of [ "true" ] -> tcerl:start ();
                                      [ "false" ] -> ok
  end,
  mnesia:change_table_copy_type (schema, node (), disc_copies),
  timer:sleep (30000)'  \
  -s erlang halt -noshell -noinput -extra "$use_tcerl" &

sleep 1

erl -pa ../src -sname fragmentron_change_target_test2 -s mnesia \
    -eval '
  case init:get_plain_arguments () of [ "true" ] -> tcerl:start ();
                                      [ "false" ] -> ok
  end,
  "fragmentron_change_target_test2@" ++ Host = atom_to_list (node ()),
  Self = node (),
  Other = list_to_atom ("fragmentron_change_target_test1@" ++ Host),
  mnesia:change_config (extra_db_nodes, [ Other ]),
  mnesia:change_table_copy_type (schema, node (), disc_copies),
  timer:sleep (30000)'  \
  -s erlang halt -noshell -noinput -extra "$use_tcerl" &

sleep 1

erl -pa ../src -sname fragmentron_change_target_test3 -s mnesia -eval '
  case init:get_plain_arguments () of [ "true" ] -> tcerl:start ();
                                      [ "false" ] -> ok
  end,
  "fragmentron_change_target_test3@" ++ Host = atom_to_list (node ()),
  Self = node (),
  Other = list_to_atom ("fragmentron_change_target_test1@" ++ Host),
  MoreOther = list_to_atom ("fragmentron_change_target_test2@" ++ Host),
  pong = net_adm:ping (Other),
  pong = net_adm:ping (MoreOther),
  { ok, Nodes } = mnesia:change_config (extra_db_nodes, [ Other, MoreOther ]),
  mnesia:change_table_copy_type (schema, node (), disc_copies),
  timer:sleep (1000),

  { atomic, ok } = 
    case init:get_plain_arguments () of
      [ "true" ] ->
        fragmentron:create_table (warez,
                                  [ { type, { external,
                                              ordered_set,
                                              tcbdbtab } },
                                    { frag_properties, 
                                      [ { node_pool, 
                                          [ node (), Other, MoreOther ] }, 
                                        { n_fragments, 2 }, 
                                        { n_external_copies, 2 } ] } ]);
      [ "false" ] ->
        fragmentron:create_table (warez,
                                  [ { frag_properties, 
                                      [ { node_pool, 
                                          [ node (), Other, MoreOther ] }, 
                                        { n_fragments, 2 }, 
                                        { n_disc_copies, 2 } ] } ])
    end,


  mnesia:subscribe (system),

  TargetType = 
    case init:get_plain_arguments () of [ "true" ] -> n_external_copies;
                                        [ "false" ] -> n_disc_copies
    end,
  CopyType = case init:get_plain_arguments () of [ "true" ] -> external_copies;
                                                 [ "false" ] -> disc_copies
             end,

  true = fragmentron:change_targets (warez, [ { TargetType, 3 } ]),

  NewNode = 
    receive { mnesia_system_event, 
              { mnesia_user, 
                { fragmentron, add_table_copy, warez, N } } } -> N
    after 0 -> exit (nomsg)
    end,

  receive { mnesia_system_event, 
            { mnesia_user, 
              { fragmentron, add_table_copy, warez_frag2, M } } } 
          when M =/= NewNode -> ok 
  after 0 -> exit (nomsg)
  end,

  { [ Other, MoreOther, Self ], [ Other, MoreOther, Self ] } = 
    { lists:sort (mnesia:table_info (warez, CopyType)), 
      lists:sort (mnesia:table_info (warez_frag2, CopyType)) },

  false = fragmentron:change_targets (warez, [ { TargetType, 3 } ]),

  receive { mnesia_system_event, 
            { mnesia_user, 
              { fragmentron, _, warez, _ } } } -> exit (badmsg)
  after 0 -> ok
  end,

  mnesia:system_info (),

  rpc:call (Other, erlang, halt, []),
  rpc:call (MoreOther, erlang, halt, [])
' -noshell -noinput -s erlang halt -extra $use_tcerl 2>&1 > changetarget.output || { cleanup; exit 1; }

cleanup

exit 0
